package com.wolfalpha.lwtool.activity;

import java.util.List;
import com.wolfalpha.lwtool.R;
import com.wolfalpha.lwtool.adapter.MyJobAdapter;
import com.wolfalpha.lwtool.service.UserService;
import com.wolfalpha.lwtool.service.impl.UserServiceImpl;
import com.wolfalpha.lwtool.vo.LWJob;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MyJobActivity extends Activity implements OnItemClickListener {

	private JobTask mTask;
	private ProgressDialog pd;
	private ListView mjob_list;
	private Button refresh;
	private Button more;
	private List<LWJob> jobs;
	private MyJobAdapter sdapter;
	private UserService usv = null;
	private int page = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mjob);
		// 设置当前活动状态
		// Application.setFrontActivity(Application.N_HISTORY);
		usv = new UserServiceImpl();
		initView();
		initListeners();
	}

	private void loadData() {
		mTask = new JobTask();
		mTask.execute();
	}

	@Override
	public void onResume() {
		loadData();
		super.onResume();
	}

	private void initListeners() {
		mjob_list.setOnItemClickListener(this);
	}

	private void initView() {
		more = (Button) findViewById(R.id.more);
		refresh = (Button) findViewById(R.id.refresh);
		mjob_list = (ListView) findViewById(R.id.list_mjob);
		more.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				page += 1;
				loadData();
			}
		});
		refresh.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				page = 0;
				loadData();
			}
		});
	}

	private class JobTask extends AsyncTask<String, Integer, String> {
		// onPreExecute方法用于在执行后台任务前做一些UI操作
		@Override
		protected void onPreExecute() {
			if (pd == null)
				pd = new ProgressDialog(MyJobActivity.this);
			pd.setMessage("正在请求数据");
			pd.setCancelable(false);
			pd.show();
		}

		// doInBackground方法内部执行后台任务,不可在此方法内修改UI
		@Override
		protected String doInBackground(String... params) {
			try {
				jobs = usv.getPublishedList(true, true, false, page, 30);
			} catch (Exception e) {
				e.printStackTrace();
				this.cancel(true);
			}
			return null;
		}

		// onProgressUpdate方法用于更新进度信息
		@Override
		protected void onProgressUpdate(Integer... progresses) {
		}

		// onPostExecute方法用于在执行完后台任务后更新UI,显示结果
		@Override
		protected void onPostExecute(String result) {
			if (jobs != null) {
				sdapter = new MyJobAdapter(MyJobActivity.this, jobs);
				mjob_list.setAdapter(sdapter);
			}
			pd.dismiss();
		}

		// onCancelled方法用于在取消执行中的任务时更改UI
		@Override
		protected void onCancelled() {
			Toast.makeText(MyJobActivity.this, "获取失败，请检查网络", Toast.LENGTH_SHORT)
					.show();
			pd.dismiss();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent();
		intent.setClass(MyJobActivity.this, MyJobInfoActivity.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("job", jobs.get(arg2));
		intent.putExtras(bundle);
		startActivity(intent);
	}
}
